package com.baitiaojun.log.core.service.impl;


import com.baitiaojun.common.enums.impl.DataSourceTypeEnums;
import com.baitiaojun.common.enums.impl.ServiceExpEnums;
import com.baitiaojun.datasource.core.datasource.annotation.DataSource;
import com.baitiaojun.log.core.dto.SysOperationLogDTO;
import com.baitiaojun.log.core.mapper.OperatingLogMapper;
import com.baitiaojun.log.core.pojo.SysOperatingLogDO;
import com.baitiaojun.log.core.service.OperationLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 日志切面反射调用日志服务
 */
@Slf4j
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperatingLogMapper, SysOperatingLogDO> implements OperationLogService {

    @Resource
    private OperatingLogMapper operatingLogMapper;

    //用同步方法, 防止在多线程下出现主键冲突或者记录问题
    @DataSource(dataSourceTypeEnums = DataSourceTypeEnums.DEFAULT_DB)
    @Override
    public synchronized void addLog(SysOperationLogDTO sysOperationLogDTO) {
        SysOperatingLogDO sysOperatingLogDO = new SysOperatingLogDO();
        BeanUtils.copyProperties(sysOperationLogDTO, sysOperatingLogDO);
        int res = operatingLogMapper.insertLog(sysOperatingLogDO);
        if (res == 0) {
            log.warn(ServiceExpEnums.EXP_INSERT_OPER_LOG_MASTER.getName());
        }
    }
}
